package com.genius.reservation.guardPerson.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genius.reservation.admissionInformation.entity.AdmissionInformation;
import com.genius.reservation.admissionInformation.service.IAdmissionInformationService;
import com.genius.reservation.car.entity.CarInformation;
import com.genius.reservation.car.entity.ReservationStartCarInformation;
import com.genius.reservation.common.ResponseRule;
import com.genius.reservation.common.constant.Constant;
import com.genius.reservation.common.constant.ResStatusCode;
import com.genius.reservation.common.rabbitmq.RabbitMessageEntity;
import com.genius.reservation.common.rabbitmq.RabbitService;
import com.genius.reservation.guardPerson.entity.GuardPersonInformation;
import com.genius.reservation.guardPerson.service.IGuardPersonInformationService;
import com.genius.reservation.mapper.CarInformationMapper;
import com.genius.reservation.mapper.DriverInformationMapper;
import com.genius.reservation.mapper.GuardPersonInformationMapper;
import com.genius.reservation.mapper.ReservationStartCarInformationMapper;
import com.genius.reservation.plague.entity.PlagueInformation;
import com.genius.reservation.plague.service.IPlagueInformationService;
import com.genius.reservation.reservation.entity.ReservationInformation;
import com.genius.reservation.reservation.service.IReservationInformationService;
import com.genius.reservation.tools.SendWXMsg;
import com.genius.reservation.user.entity.UserInformation;
import com.genius.reservation.user.service.IUserInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 警卫人员表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2022-05-26
 */
@Service
public class GuardPersonInformationServiceImpl extends ServiceImpl<GuardPersonInformationMapper, GuardPersonInformation> implements IGuardPersonInformationService {
    @Autowired
    DriverInformationMapper driverMapper;
    @Autowired
    CarInformationMapper carMapper;
    @Autowired
    IPlagueInformationService plagueService;


    @Autowired
    IAdmissionInformationService admissService;

    @Autowired
    IReservationInformationService reservationService;

    @Autowired
    RabbitService rabbitService;

    @Autowired
    @Lazy
    IUserInformationService userService;
    @Autowired
    SendWXMsg sendWXMsg;

    @Autowired
    @Lazy
    ReservationStartCarInformationMapper startCarMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public GuardPersonInformation createGuarder(UserInformation user) {
        GuardPersonInformation guardPersonInformation = new GuardPersonInformation();
        guardPersonInformation.setGuardPersonName(user.getName());
        guardPersonInformation.setGuardPersonNumber(Constant.GUARDER + IdUtil.randomUUID());
        guardPersonInformation.setUserNumber(user.getUserNumber());
        return this.saAndUp(guardPersonInformation);
    }

//    @Override
//    public ResponseRule arrive(String reservationNumber) {
//        synchronized (this){
//
//
//        //查找订单
//        ReservationInformation one = reservationService.getByReservationNumber(reservationNumber);
//
//        if (one.getNowStatus()!= ResStatusCode.RESERVATION_SETOUT||one.getNowStatus()== ResStatusCode.RESERVATION_ARRIVE){
//            return ResponseRule.no("确认到达失败");
//        }
//        //获取预约时间
//        DateTime confirm = new DateTime(one.getConfirmArrivalTime());
//        //获取到达时间
//        DateTime actual = DateTime.now();
//        //计算时间
//        long between = DateUtil.between(actual, confirm, DateUnit.MINUTE);
//        RabbitMessageEntity<ReservationInformation> driverMessage = new RabbitMessageEntity<>();
//        RabbitMessageEntity<ReservationInformation> receiverMessage = new RabbitMessageEntity<>();
//        RabbitMessageEntity<ReservationInformation> receiveManagerMessage = new RabbitMessageEntity<>();
//        //创建入场记录表
//        AdmissionInformation ad = new AdmissionInformation();
//        //订单信息
//        ReservationInformation re;
//        //判断是早到还是迟到还是正常
//        if (actual.isBefore(confirm)) {
//            //早到
//            if (between <= -10) {
//                ad.setIsLate(0);
//                //订单设置字段
//                one.setTimeoutStatus(0);
//                one.setActualArrivalTime(actual.toString());
//                re = reservationService.actualArrivalTime(one);
//
//                receiveManagerMessage.setMsg(String.format("预约准时到达,正在前往%s号月台装卸,单号为:%s", re.getConfirmPlatform().getPlatformCode(), reservationNumber));
//                receiveManagerMessage.setReservationNumber(reservationNumber);
//                receiveManagerMessage.setData(re);
//                driverMessage.setMsg(String.format("预约准时到达,请前往%s号月台装卸,单号为:%s", re.getConfirmPlatform().getPlatformCode(), reservationNumber));
//                driverMessage.setReservationNumber(reservationNumber);
//                driverMessage.setData(re);
//                receiverMessage.setMsg(String.format("预约准时到达,请前往%s号月台装卸,单号为:%s", re.getConfirmPlatform().getPlatformCode(), reservationNumber));
//                receiverMessage.setReservationNumber(reservationNumber);
//                receiverMessage.setData(re);
//                rabbitService.ToReceiver(receiverMessage);
//            } else {
//                ad.setIsLate(2);
//                //订单设置字段
//                one.setTimeoutStatus(2);
//                one.setActualArrivalTime(actual.toString());
//                re = reservationService.actualArrivalTime(one);
//                receiveManagerMessage.setMsg(String.format("预约提前到达,准备开往等待区等待,车牌号:%s", reservationNumber));
//                receiveManagerMessage.setReservationNumber(reservationNumber);
//                receiveManagerMessage.setData(re);
//                driverMessage.setMsg(String.format("您已提前到达,请开往等待区等待重新分配月台,车牌号:%s", reservationNumber));
//                driverMessage.setReservationNumber(reservationNumber);
//                driverMessage.setData(re);
//            }
//        } else {
//            //超时
//            ad.setIsLate(1);
//            //订单设置字段
//            one.setTimeoutStatus(1);
//            one.setActualArrivalTime(actual.toString());
//            re = reservationService.actualArrivalTime(one);
//            System.out.println(re);
//            receiveManagerMessage.setMsg(String.format("预约超时到达,请重新分配月台,单号为:%s", reservationNumber));
//            receiveManagerMessage.setReservationNumber(reservationNumber);
//            receiveManagerMessage.setData(re);
//            driverMessage.setMsg(String.format("您已超时,请开往等待区等待重新分配月台,车牌号:%s", reservationNumber));
//            driverMessage.setReservationNumber(reservationNumber);
//            driverMessage.setData(re);
//        }
//        //记录表设置字段
//        ad.setReservationNumber(re.getReservationNumber());
//        ad.setDriverNumber(re.getDriverNumber());
//        ad.setAdmissionNumber(Constant.ADMISSION_PREFIX + IdUtil.randomUUID());
//        ad.setArriveTime(DateTime.now().toString());
//        ad.setAdmissionTime(DateTime.now().toString());
//        admissService.saAndUp(ad);
//
//        //发送消息给各个角色
//        rabbitService.ToReceiverManager(receiveManagerMessage);
//        rabbitService.ToDriver(driverMessage);
//        String supplierWxNumber = userService.getUserByUserNumber(one.getSupplierNumber()).getWxNumber();
//        sendWXMsg.sendMsg(supplierWxNumber,"已到达",one.getConfirmArrivalTime(),"物流预约申请");
//        return ResponseRule.ok("确认到达完成");
//        }
//    }

    @Override
    public ResponseRule findCar(String carCode)  {
        LambdaQueryWrapper<ReservationStartCarInformation> eq = startCarMapper.getWrapper().eq(ReservationStartCarInformation::getCarCode, carCode).eq(ReservationStartCarInformation::getNowStatus, ResStatusCode.RESERVATION_SETOUT).or().eq(ReservationStartCarInformation::getNowStatus,ResStatusCode.RESERVATION_ARRIVE);
        ReservationStartCarInformation carInformation1 = startCarMapper.selectOne(eq);
        if (ObjectUtil.isEmpty(carInformation1)) {
            return ResponseRule.no("此车没有预约或已入场,暂无信息");
        }
        return ResponseRule.ok(carInformation1);
    }
}
